##xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# list(PREPEND CMAKE_PREFIX_PATH "${MY_THIRD_PARTY}/fmt/lib/cmake/fmt")
# list(PREPEND CMAKE_PREFIX_PATH "${MY_THIRD_PARTY}/spdlog/lib/cmake/spdlog")


##xxxxxxxxxxxxxxxxxxxxxxxxxxxxx fmt
find_package(fmt CONFIG REQUIRED)
# 打印 fmt 库的基本信息
message(STATUS "[MYFIND] fmt_VERSION: ${fmt_VERSION}")
# 打印 fmt::fmt 目标的详细信息
if(TARGET fmt::fmt AND TRUE)
    message(STATUS "[MYFIND] Target fmt::fmt exists")

    # 获取目标类型
    get_target_property(FMT_TARGET_TYPE fmt::fmt TYPE)
    message(STATUS "[MYFIND] fmt::fmt target type: ${FMT_TARGET_TYPE}")

    # 获取包含目录
    get_target_property(FMT_INCLUDE_DIRS fmt::fmt INTERFACE_INCLUDE_DIRECTORIES)
    message(STATUS "[MYFIND] fmt::fmt include directories: ${FMT_INCLUDE_DIRS}")

    # 获取链接库
    get_target_property(FMT_LINK_LIBRARIES fmt::fmt INTERFACE_LINK_LIBRARIES)
    message(STATUS "[MYFIND] fmt::fmt link libraries: ${FMT_LINK_LIBRARIES}")

    # 获取编译定义
    get_target_property(FMT_COMPILE_DEFINITIONS fmt::fmt INTERFACE_COMPILE_DEFINITIONS)
    message(STATUS "[MYFIND] fmt::fmt compile definitions: ${FMT_COMPILE_DEFINITIONS}")

    # 获取编译选项
    get_target_property(FMT_COMPILE_OPTIONS fmt::fmt INTERFACE_COMPILE_OPTIONS)
    message(STATUS "[MYFIND] fmt::fmt compile options: ${FMT_COMPILE_OPTIONS}")
else()
    message(STATUS "[MYFIND] skip print, target fmt::fmt vars")
endif()


##xxxxxxxxxxxxxxxxxxxxxxxxxxxxx spdlog
find_package(spdlog CONFIG REQUIRED)

# 打印 spdlog 库的基本信息
if(TARGET spdlog::spdlog AND TRUE)
    message(STATUS "[MYFIND] Target spdlog::spdlog exists")

    # 获取目标类型
    get_target_property(SPDLOG_TARGET_TYPE spdlog::spdlog TYPE)
    message(STATUS "[MYFIND] spdlog::spdlog target type: ${SPDLOG_TARGET_TYPE}")

    # 获取包含目录
    get_target_property(SPDLOG_INCLUDE_DIRS spdlog::spdlog INTERFACE_INCLUDE_DIRECTORIES)
    message(STATUS "[MYFIND] spdlog::spdlog include directories: ${SPDLOG_INCLUDE_DIRS}")

    # 获取链接库
    get_target_property(SPDLOG_LINK_LIBRARIES spdlog::spdlog INTERFACE_LINK_LIBRARIES)
    message(STATUS "[MYFIND] spdlog::spdlog link libraries: ${SPDLOG_LINK_LIBRARIES}")

    # 获取编译定义
    get_target_property(SPDLOG_COMPILE_DEFINITIONS spdlog::spdlog INTERFACE_COMPILE_DEFINITIONS)
    message(STATUS "[MYFIND] spdlog::spdlog compile definitions: ${SPDLOG_COMPILE_DEFINITIONS}")

    # 获取编译选项
    get_target_property(SPDLOG_COMPILE_OPTIONS spdlog::spdlog INTERFACE_COMPILE_OPTIONS)
    message(STATUS "[MYFIND] spdlog::spdlog compile options: ${SPDLOG_COMPILE_OPTIONS}")
else()
    message(STATUS "[MYFIND] skip print, target spdlog::spdlog vars")
endif()


##xxxxxxxxxxxxxxxxxxxxxxxxxxxxx cuda
if(TRUE)
    find_package(CUDAToolkit REQUIRED)
endif()

# 打印 CUDA 的基本信息
if(CMAKE_CUDA_COMPILER AND TRUE)
    message(STATUS "[CUDA] CUDA compiler found: ${CMAKE_CUDA_COMPILER}")
    message(STATUS "[CUDA] CUDA toolkit directory: ${CMAKE_CUDA_TOOLKIT_ROOT_DIR}")
    message(STATUS "[CUDA] CUDA include directories: ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}")
    message(STATUS "[CUDA] CUDA host compiler: ${CMAKE_CUDA_HOST_COMPILER}")
    message(STATUS "[CUDA] CUDA standard libraries: ${CMAKE_CUDA_STANDARD_LIBRARIES}")

    # 获取 CUDA 版本信息
    if(DEFINED CMAKE_CUDA_COMPILER_VERSION)
        message(STATUS "[CUDA] CUDA compiler version: ${CMAKE_CUDA_COMPILER_VERSION}")
    endif()

    # 获取 CUDA 架构信息
    if(DEFINED CMAKE_CUDA_ARCHITECTURES)
        message(STATUS "[CUDA] CUDA architectures: ${CMAKE_CUDA_ARCHITECTURES}")
    endif()

    # 打印 CUDA 库信息
    message(STATUS "[CUDA] CUDA_LIBRARIES: ${CUDA_LIBRARIES}")

    # 检查特定 CUDA 库
    if(DEFINED CUDA_cudart_LIBRARY)
        message(STATUS "[CUDA] CUDA cudart library: ${CUDA_cudart_LIBRARY}")
    endif()

    if(DEFINED CUDA_cublas_LIBRARY)
        message(STATUS "[CUDA] CUDA cublas library: ${CUDA_cublas_LIBRARY}")
    endif()

    if(DEFINED CUDA_cufft_LIBRARY)
        message(STATUS "[CUDA] CUDA cufft library: ${CUDA_cufft_LIBRARY}")
    endif()

    if(DEFINED CUDA_cufft64_LIBRARY)
        message(STATUS "[CUDA] CUDA cufft64 library: ${CUDA_cufft64_LIBRARY}")
    endif()

    # 打印所有 CUDA 相关的变量
    if(FALSE)
        get_cmake_property(VAR_NAMES VARIABLES)
        foreach(VAR_NAME ${VAR_NAMES})
            if(VAR_NAME MATCHES "^CUDA_")
                message(STATUS "[CUDA] CUDA variable ${VAR_NAME}: ${${VAR_NAME}}")
            endif()
        endforeach()
    endif()

else()
    message(STATUS "[CUDA] skip print CUDA")
endif()


##xxxxxxxxxxxxxxxxxxxxxxxxxxxxx cnpy库. 读取 .npz 文件
find_package(cnpy CONFIG REQUIRED)


#  find_package(clipp CONFIG REQUIRED)
find_package(argparse REQUIRED)


##xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx python
list(PREPEND CMAKE_PREFIX_PATH "C:/Users/tom/AppData/Local/Programs/Python/Python312")
find_package(Python COMPONENTS Interpreter Development Development.SABIModule)


##xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx pybind11
find_package(pybind11 REQUIRED)
message(STATUS "[python] Python_EXECUTABLE -> ${Python_EXECUTABLE}")
message(STATUS "[python] Python3_SOABI: ${Python3_SOABI}")

##xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx pytorch
##torch cmake folder
# 查找Torch安装目录的CMake代码
# 支持Windows和Linux平台
# 初始化Torch安装目录变量
if(FALSE)
    set(my_TORCH_PKG_DIR "")
    # 根据不同平台设置候选路径
    if(WIN32)
    ## Windows平台候选路径
        set(torch_candidate_paths
            "C:/cpplibs/libtorch"
            "$ENV{LOCALAPPDATA}/Programs/Python/Python312/Lib/site-packages/torch"
            # 增加Python常见版本路径
            "$ENV{LOCALAPPDATA}/Programs/Python/Python311/Lib/site-packages/torch"
        )
    elseif(UNIX AND NOT APPLE)
    ## Linux平台候选路径
        set(torch_candidate_paths
            "/usr/local/lib/python3.12/dist-packages/torch"
            "/usr/lib/python3/dist-packages/torch"
            "$ENV{HOME}/.local/lib/python3.12/site-packages/torch"
            "/usr/local/lib/python3.11/dist-packages/torch"
            "/usr/lib/python3.11/dist-packages/torch"
            "$ENV{HOME}/.local/lib/python3.11/site-packages/torch"
        )
    endif()
    ## 检查每个候选路径
    foreach(path IN LISTS torch_candidate_paths)
        if(EXISTS "${path}")
            set(my_TORCH_PKG_DIR "${path}")
            message(STATUS "Torch package directory found: ${my_TORCH_PKG_DIR}")
            break()
        endif()
    endforeach()

    ## 如果未找到，给出提示
    if(NOT my_TORCH_PKG_DIR)
        message(FATAL_ERROR "Torch CMake directory missing, please ensure PyTorch installed")
    endif()

    message(STATUS "[torch] CUDA_nvrtc_LIBRARY => ${CUDA_nvrtc_LIBRARY}")
    # set(TORCH_CUDA_ARCH_LIST "8.6")
    list(PREPEND CMAKE_PREFIX_PATH "${my_TORCH_PKG_DIR}/share/cmake")


    ##======== find pytorch
    find_package(Torch REQUIRED)
    message(STATUS "[torch] TORCH_INCLUDE_DIRS => ${TORCH_INCLUDE_DIRS}")
    message(STATUS "[torch] TORCH_LIBRARIES => ${TORCH_LIBRARIES}")
    message(STATUS "[torch] TORCH_CXX_FLAGS => ${TORCH_CXX_FLAGS}")
    message(STATUS "[torch] Caffe2_MAIN_LIBS => ${Caffe2_MAIN_LIBS}")
    message(STATUS "[torch] CAFFE2_INCLUDE_DIRS => ${CAFFE2_INCLUDE_DIRS}")
    message(STATUS "[torch] ATEN_INCLUDE_DIR => ${ATEN_INCLUDE_DIR}")
    message(STATUS "[torch] ATEN_LIBRARIES => ${ATEN_LIBRARIES}")

    get_target_property(tmpl torch INTERFACE_LINK_LIBRARIES)
    message(STATUS "[torch] torch.INTERFACE_LINK_LIBRARIES ->${tmpl}")

    get_target_property(tmpl torch_cpu_library INTERFACE_LINK_LIBRARIES)
    message(STATUS "[torch] torch_cpu_library.INTERFACE_LINK_LIBRARIES ->${tmpl}")

    get_target_property(tmpl torch_cuda_library INTERFACE_LINK_LIBRARIES)
    message(STATUS "[torch] torch_cuda_library.INTERFACE_LINK_LIBRARIES ->${tmpl}")

    ##====================================== 
    set(CMAKE_CUDA_ARCHITECTURES native)
    message(STATUS "[torch] CMAKE_CUDA_FLAGS ->${CMAKE_CUDA_FLAGS}")
    message(STATUS "[torch] CMAKE_CUDA_ARCHITECTURES => ${CMAKE_CUDA_ARCHITECTURES}")
endif()